﻿using System;
using System.ServiceModel;
using DemoLib.Utilities.LoggingUtilities;
using DemoLib.Utilities.Properties;

namespace DemoLib.Utilities.ServiceUtilities
{
    public class GenericServiceHost<T> : ServiceHost
        where T : class
    {
        public GenericServiceHost()
            : base(typeof (T))
        {
        }

        protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
        {
            Log.LogInformation(string.Format(Resources.StartingService, typeof (T).FullName));
            return base.OnBeginOpen(timeout, callback, state);
        }

        protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
        {
            Log.LogInformation(string.Format(Resources.StopingService, typeof (T).FullName));
            return base.OnBeginClose(timeout, callback, state);
        }

        protected override void OnAbort()
        {
            Log.LogWarning(string.Format(Resources.AbortingSerivce, typeof (T).FullName));
            base.OnAbort();
        }

        protected override void OnFaulted()
        {
            Log.LogError(string.Format(Resources.ServiceFault, typeof (T).FullName));
            base.OnFaulted();
        }
    }
}
